const {
  request
} = require("../../utils/index.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    atyId: "", //当前所点击的帖子的id
    detail: {}, //商业共享详情数据
    showView: true,
    longago:'',  //几小时前
    getnowtime:new Date().getTime(),   //当前时间
    getreleasetime:'',  //发布时间
    word:true,  //评论展开
    comments:[],  //评论列表数据
    inputValue:"",  //评论内容
    show: false, //留言框显示
    min: 0,
    max: 500,
    currentWordNumber: '',
    Message: "请输入留言", //留言板内容
  },
  inputs: function (e) {
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
  },
  onClickShow() {
    this.setData({
      show: true,
      isActive: true,
    });
  },

  onClickHide() {
    this.setData({
      show: false,
      isActive: false
    });
  },
  // 留言板内容
  Message(e) {
    this.setData({
      Message: e.detail.value
    })
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
  },
  //获得焦点时清空多文本框
  qk(){
    console.log(1232)
    this.setData({
      Message:"",
      currentWordNumber: 0
    })
  },

  async messSub() {
    let that = this
    if (this.data.Message == "") {
      wx.showToast({
        title: '请输入留言',
        icon: "none"
      })
    } else {
      let par = {
        url: 'hw/portal/leaving/save?token=' + wx.getStorageSync('token'),
        data: {
          articleId: that.data.atyId,
          articleTitle: that.data.detail.title,
          lmContent: that.data.Message,
          lmModule: "activity_call"
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
        },
      }
      var result = await request(par);
      if (result.status == 100) {
        wx.showToast({
          title: '留言成功！',
        })
        //提交后清空数据
        that.setData({
          Message: "",
          currentWordNumber: 0
        })
        this.onClickHide()
      } else {
        wx.showToast({
          title: result.message,
          icon: 'none'
        })
      }
    }
  },
  //评论展开或收起
  word:function(){
    this.setData({
      word: !this.data.word
  })
  console.log(this.data.word)
  },
    //评论
    async getpinglun() {
      var that = this;
      let par = {
        data: {
          cmtModule: 'activity_call', //需要查看评论对象
          // pageNum: 1, //页码
          // pageSize: 5, //每页条数
          objId: that.options.atyId, //需要查看评论对象的ID
        },
        url: 'hw/portal/comment/page?token=' + wx.getStorageSync('token'),
        method: "get"
      }
      let res = await request(par);
      if (res.status == 100) {
        this.setData({
          comments:res.data.list
        })
      }
    },
    //评论内容
    blur(e){
      this.setData({inputValue:e.detail.value});
    },
    async enter(){
      let that=this;
      if(that.data.inputValue==""){
        wx.showToast({
          title: '请填写评论内容',
          icon:"none",
        })
      }else{
      let param={
        url:"hw/portal/comment/save",
        method:"POST",
        header:{
         'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data:{
          "token":wx.getStorageSync('token'),
          cmtContent:that.data.inputValue,
          cmtModule:"activity_call",
          objId:that.options.atyId,     //id
        }
      }
      let data=await request(param);
      if(data.status==100){
        that.setData({
          inputValue:"",
          comments: [...that.data.comments, data.data]
        })
        wx.showToast({
          title: '成功',
          icon:"none",
        })
      }
      }
    },
  
    
      // 视频展示
      chooseshow: function (e) {
        var that = this;
        wx.previewMedia({
          current: 0,
          type: 'video',
          url: that.data.detail.files[0].updUrl
        })
      },
  // 点击查看图片
  handleSwiper(e) {
    var that = this;
    var urlList = [];
    var scenePics = that.data.detail.files
    for (var i = 0; i < scenePics.length; i++) {
      urlList.push(scenePics[i].updUrl);
    }
    wx.previewImage({
      urls: urlList,
      current: urlList[e.currentTarget.dataset.id]
    })
  },
    //召集收藏增加
    zjshoucang:async function(e){
      let param={
       url:"hw/portal/user/collect/save",
       header:{
        'content-type': 'application/x-www-form-urlencoded', // 默认值
       },
       data:{
         "token":wx.getStorageSync('token'),
        "collectModule":"activity_call",
        "objId":this.data.atyId,
       },
       method:"POST"
      }
      let datainfo=await request(param);
      console.log(datainfo)
      if(datainfo.status==100){
        this.setData({
          "detail.isCollect":!this.data.detail.isCollect
        })
        console.log(this.data.detail.isCollect)
      }
    },
    //召集收藏取消
    zjcanceshoucang:async function(){
      let parm={
        url:"hw/portal/user/collect/del",
        header:{
         'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data:{
         "token":wx.getStorageSync('token'),
         "collectModule":"activity_call",
         "objId":this.data.atyId,     //当前用户id
        },
        method:"POST"
       }
       let datainfo =await request(parm);
       if(datainfo.status==100){
         this.setData({
          "detail.isCollect":!this.data.detail.isCollect
         })
         console.log(this.data.detail.isCollect)
       }
    },
  // 显示隐藏
  showButton:function(){
    var that = this;
    that.setData({
      showView: false
    })
  },
  // 显示隐藏
  showButton2:function(){
    var that = this;
    that.setData({
      showView: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getpinglun();
    this.setData({
      getreleasetime:this.data.detail.createTime
    })
    console.log("当前时间"+this.data.getnowtime)
    console.log("发布时间"+this.data.getreleasetime)
    var tieaaa=this.data.getreleasetime-this.data.getnowtime
    console.log("相差时间"+tieaaa/(1000*60*60))
    let {
      atyId
    } = options
    atyId = parseInt(atyId);
    this.setData({
      atyId
    })
    this.init(atyId);
  },

  // 活动组织数据
  async init(atyId){
    let param={
      url:"hw/portal/activity/detail/"+ parseInt(atyId) + "?token=" + wx.getStorageSync('token'),
      method:"get",
    }
    let data=await request(param);
    console.log(data)
    this.setData({
      detail:data.data
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  async showButton(){
    let param={
      url:"hw/portal/activity/sign_up/"+ parseInt(this.data.atyId) + "?token=" + wx.getStorageSync('token'),
      method:"get",
    }
    let data=await request(param);
    if(data.status==100){
      wx.showToast({
        title: '报名成功',
        icon:"none",
      })
    }else{
      wx.showToast({
        title: data.message,
        icon:"none",
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})